package com.jf.cloud.product.listener;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jf.cloud.product.constant.SpuExportError;
import com.jf.cloud.product.service.SpuExcelService;
import com.jf.cloud.product.vo.SpuExcelVO;
import com.jf.cloud.product.vo.SupplierProdExcelVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class SupplierProdExcelListener extends AnalysisEventListener<SupplierProdExcelVO> {
    private SpuExcelService spuExcelService;
    private Map<Integer, List<String>> errorMap;
    private List<SupplierProdExcelVO> list;


    /**
     * 每隔1000条数据存储数据库，然后清理map ，方便内存回收
     */
    private static final int BATCH_COUNT = 3000;

    public SupplierProdExcelListener() {
    }

    public SupplierProdExcelListener(SpuExcelService spuExcelService, Map<Integer, List<String>> errorMap) {
        this.spuExcelService = spuExcelService;
        this.errorMap = errorMap;
        this.list = new ArrayList<>();
    }

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(SupplierProdExcelVO supplierProdExcelVO, AnalysisContext analysisContext) {

        boolean isSave =   list.size() > BATCH_COUNT;
        if (isSave) {
            saveData();
        }
        list.add(supplierProdExcelVO);
    }

    private void saveData() {
        List<SpuExcelVO> listSpuExcelVO = new ArrayList<>();
        try {
            if(CollUtil.isNotEmpty(list)){
                listSpuExcelVO = list.stream().map(supplierProdExcelVO -> {
                    SpuExcelVO spuExcelVO = new SpuExcelVO();
                    spuExcelVO.setSeq(supplierProdExcelVO.getSeq());
                    spuExcelVO.setNameZh(supplierProdExcelVO.getNameZh());
//                    spuExcelVO.setNameEn(supplierProdExcelVO.getNameEn());
                    spuExcelVO.setImgs(supplierProdExcelVO.getImgs());
                    spuExcelVO.setVideo(supplierProdExcelVO.getVideo());
                    spuExcelVO.setSellingPointZh(supplierProdExcelVO.getSellingPointZh());
//                    spuExcelVO.setSellingPointEn(supplierProdExcelVO.getSellingPointEn());
                    spuExcelVO.setBrandName(supplierProdExcelVO.getBrandName());
                    spuExcelVO.setCategoryName(supplierProdExcelVO.getCategoryName());
                    spuExcelVO.setShopCategoryName(supplierProdExcelVO.getShopCategoryName());
                    spuExcelVO.setDeliveryMode(supplierProdExcelVO.getDeliveryMode());
                    spuExcelVO.setDeliveryTemplate(supplierProdExcelVO.getDeliveryTemplate());
                    spuExcelVO.setStatus(supplierProdExcelVO.getStatus());
                    spuExcelVO.setWeight(Objects.isNull(supplierProdExcelVO.getWeight()) ? 0.0 : supplierProdExcelVO.getWeight());
                    spuExcelVO.setVolume(Objects.isNull(supplierProdExcelVO.getVolume()) ? 0.0 : supplierProdExcelVO.getVolume());
                    spuExcelVO.setPriceFee(supplierProdExcelVO.getPriceFee());
                    spuExcelVO.setMarketPriceFee(supplierProdExcelVO.getMarketPriceFee());
                    spuExcelVO.setStock(supplierProdExcelVO.getStock());
                    spuExcelVO.setSpuCode(Objects.nonNull(supplierProdExcelVO.getSpuCode())? supplierProdExcelVO.getSpuCode() : null);
                    spuExcelVO.setPropertiesZh(Objects.nonNull(supplierProdExcelVO.getPropertiesZh()) ? supplierProdExcelVO.getPropertiesZh():null);
//                    spuExcelVO.setPropertiesEn(Objects.nonNull(supplierProdExcelVO.getPropertiesEn()) ? supplierProdExcelVO.getPropertiesEn() : null);
                    spuExcelVO.setSkuNameZh(Objects.nonNull(supplierProdExcelVO.getSkuNameZh()) ? supplierProdExcelVO.getSkuNameZh() : null);
//                    spuExcelVO.setSkuNameEn(Objects.nonNull(supplierProdExcelVO.getSkuNameEn()) ? supplierProdExcelVO.getSkuNameEn() : null);
                    spuExcelVO.setStockWarning(supplierProdExcelVO.getStockWarning());
                    return spuExcelVO;
                }).collect(Collectors.toList());
            }

            spuExcelService.exportExcel(listSpuExcelVO, errorMap);
        } catch (Exception e) {
            List list = errorMap.get(SpuExportError.OTHER.value());
            if (CollUtil.isEmpty(list)) {
                list = new ArrayList();
                errorMap.put(SpuExportError.OTHER.value(), list);
            }
            list.add(e.getMessage());
        }
        list.clear();
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //确保所有数据都能入库
        saveData();
    }
}
